//
//  FKDownloadTableViewController.m
//  FKNetWorking
//
//  Created by 云眼科技 on 16/7/18.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "FKDownloadTableViewController.h"
#import "FKTableViewCellStyleSet.h"
#import "FKNetworking/FKNetworkingManager.h"
#import "NSObject+FKAdd.h"
#import "NSString+FKAdd.h"

@interface FKDownloadTableViewController () <FKDownloadCellDelegate>{
    FKNetworkingManager *manager;
    NSMutableArray *fooArray;
}
@property (nonatomic, strong) NSMutableArray <FKNetworkingDownloadModel *> *dataArray;

@end

NSString *const download_url = @"http://www.yunyankeji.com:1222/dzy/Link/resource/6938888888615.zip";
NSString * const downloadUrl1 = @"http://baobab.wdjcdn.com/14525705791193.mp4";

static NSString *const download_identifier = @"fk_download_identifier"; 

@implementation FKDownloadTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@", NSHomeDirectory());

    fooArray = [NSMutableArray arrayWithArray:@[download_url, downloadUrl1]];
    
    manager = [FKNetworkingManager shareManager];
    
    
    
    
    
    
    self.tableView.rowHeight = 70.f;
    [self.tableView registerClass:[FKDownloadTableViewCell class] forCellReuseIdentifier:download_identifier];
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FKDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:download_identifier forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.delegate = self;
    FKNetworkingDownloadModel *model = manager.downloadModelsDict[fooArray[indexPath.row]];
    cell.nameLabel.text = model.fileName;
    cell.progressView.progress = model.progressModel.downloadProgress;
    
    
    
    [cell.downloadBtn setTitle:FKStateDescriptionStart_CH forState:UIControlStateNormal];
    return cell;
}

-(void)FKDownloadCellDidPressedButtonForIndexPath:(NSIndexPath *)indexPath{
    
    FKDownloadTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.downloadBtn.currentTitle == FKStateDescriptionStart_CH || cell.downloadBtn.currentTitle == FKStateDescriptionContinue_CH || cell.downloadBtn.currentTitle == FKStateDescriptionCompleted_CH) {
        [cell.downloadBtn setTitle:FKStateDescriptionRunning_CH forState:UIControlStateNormal];
        
        [[FKNetworkingManager shareManager] fk_startDownloadWithDownloadModel:self.dataArray[indexPath.row] progress:^(FKNetworkingDownloadModel * _Nonnull downloadModel) {
            fk_run_main_queue(^{
                cell.progressView.progress = downloadModel.progressModel.downloadProgress; 
                cell.nameLabel.text = [NSString fk_formatFileSize:downloadModel.downloadTask.countOfBytesExpectedToReceive currentFileSize:downloadModel.downloadTask.countOfBytesReceived progress:downloadModel.progressModel.downloadProgress];
                cell.speedLabel.text = [NSString fk_formatSpeed:downloadModel.progressModel.downloadSpeed];
                cell.leftTimeLabel.text = [NSString fk_formatLeftTime:downloadModel.progressModel.downloadLeft];
            });
        } completionHandler:^(FKNetworkingDownloadModel * _Nonnull downloadModel, NSError * _Nonnull error) {
            if (error) {
                NSLog(@"%@", error.localizedDescription);
                return ;
            }
            [cell.downloadBtn setTitle:FKStateDescriptionCompleted_CH forState:UIControlStateNormal];
        }];

    }else if (cell.downloadBtn.currentTitle == FKStateDescriptionRunning_CH){
        [[FKNetworkingManager shareManager] fk_cancelDownloadTaskWithDownloadModel:self.dataArray[indexPath.row]];
        fk_run_main_queue(^{
            [cell.downloadBtn setTitle:FKStateDescriptionContinue_CH forState:UIControlStateNormal];
        });
    }
}

-(NSMutableArray <FKNetworkingDownloadModel *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
        FKNetworkingDownloadModel *model1 = [[FKNetworkingDownloadModel alloc] initWithResourceURLString:downloadUrl1];
        FKNetworkingDownloadModel *model2 = [[FKNetworkingDownloadModel alloc] initWithResourceURLString:download_url];

        [_dataArray addObject:model1];
        [_dataArray addObject:model2];
    }
    return _dataArray;
}

@end
